<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 * 制造卡号，提供加密还原，等功能
 * @name    MakecardNo
 * @see  
 * @version	1.0.0 (Tue Nov 11 13:33:17 CST 2008)
 * @author	sanshi
 */
 
class MakecardNo
{
   /**
	* 构造函数
	* @return void 
	* @access public
	*/
    private $sanshi_set_key_str= "sanshi";
	public function __construct()
	{
		$this->MakecardNo();
	}
	public function getInterferon()
	{
		return $this->sanshi_set_key_str;
	}
	public function MakecardNo()
	{
	}
	/**
	 * 取得随机数 当前是 11位，首位 为0
	 * */
	public  function makeNo()
	{
		return sprintf('%011d',mt_rand(1,9999999999));
	}
	/**
	 * 提供卡号。取得伪码 16位
	 * */
	public  function getPass_by_cardNo($card_no)
	{
		return self::sanshi_StrCode($card_no,'ENCODE');
	}
	/**
	 * 通过伪码，取得卡号
	 * */
	public function getcardNo_by_cardPass($card_pass)
	{
		return self::sanshi_StrCode($card_pass);
	}
	/**
	 * */
	public function getcardPwd_by_cardPass($card_pass)
	{
		return substr($card_pass,3,8);
	}
	/**
	 * 可逆加密算法
	 * */
	private function sanshi_StrCode($string,$action='DECODE')
	{
		$key = $this->sanshi_set_key_str;
		$string	= $action == 'ENCODE' ? $string : base64_decode($string);
		$len	= strlen($key);
		$code	= '';
		for($i=0; $i<strlen($string); $i++)
		{
			$k		= $i % $len;
			$code  .= $string[$i] ^ $key[$k];
		}
		$code = $action == 'DECODE' ? $code : base64_encode($code);
		return $code;
	}
	
}
/*
$m = new MakecardNo();
$card_no = $m->makeNo();
$card_pass = $m->getPass_by_cardNo('p'.$card_no);
$card_pwd = $m->getcardPwd_by_cardPass($card_pass);
echo $card_no.'->'.$card_pass.'->'.$card_pwd."</br>";
*/
?>